/*
 * @Author: ygnnli 1585923072@qq.com
 * @Date: 2024-08-15 20:21:39
 * @LastEditors: ygnnli 1585923072@qq.com
 * @LastEditTime: 2024-08-19 15:57:33
 * @FilePath: /bitrpc/source/test_3.cpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AEmessage.hpp
 */

#include "../common/dispatcher.hpp"
#include "../server/rpc_router.hpp"
#include <thread>

// void onRpcRequest(const bitrpc::BaseConnection::ptr& conn, bitrpc::RpcRequest::ptr& msg)
// {
//     std::cout<<"收到了Rpc请求: "<<msg->method()<<std::endl;
//     std::string body = msg->serialize();
//     std::cout<<body<<std::endl;
//     std::cout<<"method: "<<msg->method()<<std::endl;
//     auto rpc_req = bitrpc::MessageFactory::create<bitrpc::RpcResponse>();
//     rpc_req->setId("111111");
//     rpc_req->setMType(bitrpc::MType::RSP_RPC);
//     rpc_req->setRCode(bitrpc::RCode::RCODE_OK);
//     rpc_req->setResult(33);
//     conn->send(rpc_req);
// }

// void onTopicRequest(const bitrpc::BaseConnection::ptr& conn, bitrpc::TopicRequest::ptr& msg)
// {
//     std::cout<<"收到了Topic请求: ";
//     std::string body = msg->serialize();
//     std::cout<<body<<std::endl;
//     auto rpc_req = bitrpc::MessageFactory::create<bitrpc::TopicResponse>();
//     rpc_req->setId("22222222");
//     rpc_req->setMType(bitrpc::MType::RSP_TOPIC);
//     rpc_req->setRCode(bitrpc::RCode::RCODE_OK);
//     conn->send(rpc_req);
// }

void Add(const Json::Value &req, Json::Value &rsp)
{
    int num1 = req["num1"].asInt();
    int num2 = req["num2"].asInt();
    rsp = num1 + num2; // 计算结果
}

int main()
{
    auto router = std::make_shared<bitrpc::server::RpcRouter>();                                           // 创建RpcRouter对象
    std::unique_ptr<bitrpc::server::SDescribeFactory> desc_factor(new bitrpc::server::SDescribeFactory()); // 创建SDescribeFactory对象
    desc_factor->setMethodName("Add");
    desc_factor->setParamsDesc("num1", bitrpc::server::VType::INTEGRAL); // 设置方法名和参数描述
    desc_factor->setParamsDesc("num2", bitrpc::server::VType::INTEGRAL);
    desc_factor->setReturnType(bitrpc::server::VType::INTEGRAL); // 设置返回值类型
    desc_factor->setCallback(Add);                               // 设置回调函数
    router->registerMethod(desc_factor->build());                // 注册方法

    auto cb = std::bind(&bitrpc::server::RpcRouter::onRpcRequest, router.get(), std::placeholders::_1, std::placeholders::_2); // router.get()获取RpcRouter对象的指针 绑定RpcRouter对象的onRpcRequest函数

    auto dispatcher = std::make_shared<bitrpc::Dispatcher>();                    // 创建Dispatcher对象 用于注册服务
    dispatcher->registerHandler<bitrpc::RpcRequest>(bitrpc::MType::REQ_RPC, cb); // 注册映射关系消息处理函数

    auto server = bitrpc::ServerFactory::create(9090);

    auto message_cb = std::bind(&bitrpc::Dispatcher::onMessage, dispatcher.get(), std::placeholders::_1, std::placeholders::_2); // dispatcher.get()获取Dispatcher对象的指针
    server->setMessageCallback(message_cb);
    server->start();

    // auto dispatcher = std::make_shared<bitrpc::Dispatcher>();//创建Dispatcher对象
    // dispatcher->registerhandler<bitrpc::RpcRequest>(bitrpc::MType::REQ_RPC, onRpcRequest);//注册映射关系消息处理函数
    // dispatcher->registerhandler<bitrpc::TopicRequest>(bitrpc::MType::REQ_TOPIC, onTopicRequest);//注册映射关系消息处理函数

    // auto server = bitrpc::ServerFactory::create(9090);

    // auto message_cb = std::bind(&bitrpc::Dispatcher::onMessage,dispatcher.get(), std::placeholders::_1, std::placeholders::_2);//dispatcher.get()获取Dispatcher对象的指针
    // server->setMessageCallback(message_cb);
    // server->start();

    return 0;
}